﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Logic;
using System.Web.Management;
using Model;

namespace web_CategoryMS.View.G02
{
    public partial class FS0201 : System.Web.UI.Page
    {
        FS0201Logic fS0201Logic = new FS0201Logic();
        
        #region 初始化事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    #region 绑定需求种类
                    DataTable dt = fS0201Logic.getCategoryList();
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        ddlCategory.Items.Clear();
                        ddlCategory.Items.Add("请选择");
                        ddlCategory.Items[0].Value = "";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            ddlCategory.Items.Add(dt.Rows[i][0].ToString());
                        }
                    }
                    else
                    {
                        ddlCategory.Items.Clear();
                        ddlCategory.Items.Add("请选择");
                        ddlCategory.Items[0].Value = "";
                    }
                    #endregion
                }
                catch (Exception ex)
                {

                }

            }
        }
        #endregion

        #region 检索事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Search();
            }
            catch (Exception ex)
            {

            }
        }
        #endregion

        #region 检索方法
        private void Search()
        {
            DataTable dt = fS0201Logic.Search(ddlCategory.SelectedValue.Trim(), txtRequirementNo.Text.Trim());
            if (dt != null && dt.Rows.Count > 0)
            {
                dgv.DataSource = dt;
                dgv.DataBind();
            }
            else
            {
                BindNullDT();
            }
        }
        #endregion

        #region 绑定空行
        private void BindNullDT()
        {

            DataTable dt = new DataTable();
            dt.Rows.Clear();
            dt.Columns.Add("iAutoID");
            dt.Columns.Add("vcRequirementNo");
            dt.Columns.Add("vcCategory");
            dt.Columns.Add("vcRequirementName");
            dt.Columns.Add("vcRequirementDescription");
            DataRow dr = dt.NewRow();
            dr["iAutoID"] = "";
            dr["vcRequirementNo"] = "";
            dr["vcCategory"] = "";
            dr["vcRequirementName"] = "";
            dr["vcRequirementDescription"] = "";
            dt.Rows.Add(dr);
            dgv.DataSource = dt;
            dgv.DataBind();
        }
        #endregion

        #region 全选事件
        protected void btnAll_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow gvr in dgv.Rows)
                {
                    ((CheckBox)gvr.Cells[0].FindControl("ChkItem")).Checked = true;
                }
            }
            catch (Exception ex)
            {

            }
        }
        #endregion

        #region 添加到购物车
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                #region 获取所选项
                List<string> shopList = new List<string>();
                foreach (GridViewRow gvr in dgv.Rows)
                {
                    if (((CheckBox)gvr.Cells[0].FindControl("ChkItem")).Checked)
                    {
                        string iAutoID = gvr.Cells[1].Text;
                        if (!string.IsNullOrEmpty(iAutoID))
                        {
                            if (shopList == null)
                            {
                                shopList = new List<string>();
                            }
                            shopList.Add(iAutoID);
                        }
                    }
                }
                #endregion
                #region 保存到购物车
                fS0201Logic.addShopList(shopList, ((sUser)Session["LoginInfo"]).vcUserID);
                #endregion
                #region 保存成功提示
                ScriptManager.RegisterStartupScript(this, typeof(Page), "", "alert('添加成功，请前往购物车查看！');", true);
                #endregion
            }
            catch (Exception ex)
            {

            }
            
        }
        #endregion

        #region 行绑定事件
        protected void dgv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (string.IsNullOrEmpty(e.Row.Cells[1].Text) || e.Row.Cells[1].Text == "&nbsp;")
                    {
                        return;
                    }
                    e.Row.Cells[4].Attributes.Add("onclick", "return OpenImage('" + e.Row.Cells[1].Text + "');");
                }
            }
            catch (Exception ex)
            {

            }
        }
        #endregion
    }
}